package com.lovo.stock.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.stock.bean.JurisdictionBean;
import com.lovo.stock.bean.PageInfo;
import com.lovo.stock.bean.RoleBean;
import com.lovo.stock.bean.UserBean;
import com.lovo.stock.constant.MessageCode;
import com.lovo.stock.service.IJurisdictionService;
import com.lovo.stock.service.IRoleService;
import com.lovo.stock.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Role;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色控制层
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    IRoleService roleService;

    @Autowired
    IJurisdictionService jurisdictionService;

    /**
     * 分页查询角色列表
     * @param pageInfo
     * @return
     */
    @RequestMapping("/queryRoleByPage")
    public ResponseResult<IPage<RoleBean>> queryRoleByPage(@RequestBody PageInfo<RoleBean> pageInfo){
        ResponseResult<IPage<RoleBean>> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(roleService.queryRoleByPage(pageInfo));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 查看所有角色
     * @return
     */
    @RequestMapping("/queryAllRole")
    public ResponseResult<List<RoleBean>> queryAllRole(){
        ResponseResult<List<RoleBean>> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(roleService.queryAllRole());
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }


    /**
     * 查看单角色及其权限
     * @param id
     * @return
     */
    @RequestMapping("/queryRoleById")
    public ResponseResult<RoleBean> queryRoleById(int id){
        ResponseResult<RoleBean> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(roleService.queryRoleById(id));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }


    /**
     * 添加角色
     * @param role
     */
    @RequestMapping("/addRole")
    public ResponseResult addRole(String jurIds,String role){
        List<Integer> list=JSONObject.parseArray(jurIds, Integer.class);
        RoleBean roleBean=JSONObject.parseObject(role, RoleBean.class);
        ResponseResult responseResult=new ResponseResult<>();
        try {
            roleService.addRole(list,roleBean);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 删除角色
     * @param id
     */
    @RequestMapping("/deleteRoleById")
    public ResponseResult deleteRoleById(int id){
        ResponseResult responseResult=new ResponseResult<>();
        try {
            roleService.deleteRoleById(id);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 修改角色权限
     * @param roleId
     * @param jurIds
     */
    @RequestMapping("/updateRole")
    @Transactional
    public ResponseResult updateRole(int roleId, String jurIds){
        List<Integer> jurIdList= JSONObject.parseArray(jurIds,Integer.class);
        ResponseResult responseResult=new ResponseResult<>();
        try {
            roleService.updateRole(roleId, jurIdList);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 根据角色id组查出权限
     * @param roleIds
     * @return
     */
    @RequestMapping("/queryJurisdictionByRoleIds")
    public ResponseResult<List<JurisdictionBean>> queryJurisdictionByRoleIds(@RequestBody List<Integer> roleIds){

        ResponseResult<List<JurisdictionBean>> responseResult=new ResponseResult<>();
        try {
            if (roleIds.size()>0){
                responseResult.setData(jurisdictionService.queryJurisdictionByRoleIds(roleIds));
            }
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 显示全部权限
     * @return
     */
    @RequestMapping("/queryJurisdiction")
    public ResponseResult<List<JurisdictionBean>> queryJurisdiction(){
        ResponseResult<List<JurisdictionBean>> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(jurisdictionService.queryJurisdiction());
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }


}
